package ai.peanut.selfbms.controller.activity;


import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.redis.Redis;
import ai.peanut.selfbase.modules.activity.entity.Activity;
import ai.peanut.selfbase.modules.activity.pojo.ActivityParam;
import ai.peanut.selfbase.modules.activity.service.IActivityService;
import ai.peanut.selfbase.modules.activity.service.impl.ActivityServiceImpl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lichong
 * @since 2018-11-09
 */
@Controller
@Slf4j
@RequestMapping("/activity")
public class ActivityController {
    @Resource
    private IActivityService iActivityService ;
    @Resource
    private Redis redis ;
    @Resource
    private SecurityManager securityManager ;


    @RequestMapping(value = "list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request, HttpServletResponse response, ActivityParam param) {
        Map<String, Object> map=new HashMap<String, Object>();
//        redis.sadd("aaaa","dsfdsf");
//        redis.expire("aaa",10000);
        try {
            String pageNoStr =  request.getParameter("page") ;
            String rowsStr =  request.getParameter("rows") ;
            Integer pageNo = 1 ;
            Integer rows = 20 ;
            if(StringUtils.isNotEmpty(pageNoStr )) {
                pageNo = Integer.parseInt(pageNoStr);
            }
            if(StringUtils.isNotEmpty(rowsStr )) {
                rows = Integer.parseInt(rowsStr);
            }
            Page page = new Page(pageNo,rows) ;
            page.setOrderByField( "id" ) ;
            page.setAsc( false ) ;
            System.out.print("3333333");
            page = iActivityService.selectActivity(page,param ) ;

            map.put("total", page.getTotal() ) ;
            map.put("rows", page.getRecords()) ;
        } catch (Exception e) {
            System.out.print("3333331111111113 ");
        }
        return map ;
    }

    @RequestMapping("/form/{id}")
    @ResponseBody
    public DataResult getResources(@PathVariable("id") Long id){
        try {
            return DataResult.success(iActivityService.selectById( id ) , Activity.class);
        } catch (Exception e){
            System.out.print("222222222222");
        }
        return null ;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DataResult saveOrUpdate(@RequestBody @Validated ActivityParam activityParam){
        Boolean bool = null;
        try {
            Activity activity = new Activity() ;
            BeanUtils.copyProperties( activityParam, activity);
            if (activity.getId() == null){
                bool = iActivityService.insert(activity);
            } else{
                bool = iActivityService.updateAllColumnById(activity);
            }
            return DataResult.success(bool, Integer.class);
        } catch (Exception e){
            System.out.print("55555555555");
        }
        return null ;
    }

    @RequestMapping("/testShiro")
    @ResponseBody
    public DataResult testShiro(){
        Boolean bool = null;
        try {
           SecurityUtils.setSecurityManager(securityManager);


            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
            subject.login( token );
            String a = "" ;

            PrincipalCollection principalCollection = subject.getPrincipals();

            System.out.println( principalCollection.getRealmNames().size());
            System.out.println(subject.isPermitted("user1:*") );
            System.out.println(subject.isPermitted("user:*") );

            Session session = subject.getSession();
            a = "" ;
            return DataResult.success(bool, Integer.class);
        } catch (Exception e){
           log.info("okokokokok",e);
        }
        return null ;
    }

    public static void main(String [] args ) {
        System.out.print("3333333");
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-context.xml"});
        BeanFactory factory = (BeanFactory) context;
        ActivityServiceImpl userservice = (ActivityServiceImpl) factory.getBean("activityServiceImpl");
        System.out.print("33333331111111");
        System.out.println(userservice.selectList(new Wrapper<Activity>() {
            @Override
            public String getSqlSegment() {
                return null;
            }
        }) );
        System.out.print("3333333");
    }


}

